//
//  RegistDetailViewController.m
//  B
//
//  Created by lanou on 16/5/13.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "RegistDetailViewController.h"

#define kRegistNow @"http://app.dunkhome.com/v2/users/check_nick_name?nick_name="
#define kRegi @"http://app.dunkhome.com/v2/users"
@interface RegistDetailViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *userImage;    // 用户头像
@property (weak, nonatomic) IBOutlet UITextField *userName;     // 用户昵称
@property (weak, nonatomic) IBOutlet UIButton *boy; // 男  male
@property (weak, nonatomic) IBOutlet UIButton *girl; // 女  female
@property (weak, nonatomic) IBOutlet UIButton *secrecy; // 保密 unknow

@property (nonatomic, strong) NSString *gender;
@end

@implementation RegistDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _gender = @"unknow";  // 默认不选性别的时候为保密
}
- (IBAction)finishRegist:(id)sender { // 完成注册
    if ([self isBlankString:self.userName.text]) {
        [self.view popMessageWithTitle:@"昵称设置不能为空" postion:CENTER];
        return;
    }
    NSString *name = [self.userName.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSString *url = [NSString stringWithFormat:@"%@%@", kRegistNow,name];
    // 先GET请求 判断该昵称是否存在
    [[AFHTTPSessionManager manager] GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self.view popMessageWithTitle:responseObject[@"msg"] postion:CENTER]; // 判断昵称是否被使用的消息
        // 如果不存在 则进行完成注册请求
        [[AFHTTPSessionManager manager] POST:kRegi parameters:@{@"gender":_gender, @"nick_name":_userName.text, @"password":_password, @"phone":_phone} progress:^(NSProgress * _Nonnull uploadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"%@", responseObject); // 注册成功
            [self dismissViewControllerAnimated:YES completion:^{
                [[NSUserDefaults standardUserDefaults] setObject:responseObject[@"nick_name"] forKey:@"nickName"];
                [[NSUserDefaults standardUserDefaults] setObject:@"登录" forKey:@"result"];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"error%@", error);
        }];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error%@", error);
    }];
}
- (IBAction)boy:(id)sender {
    if (_boy.selected) {
        [_boy setImage:[UIImage imageNamed:@"gender_default_btn.png"] forState:UIControlStateNormal];
        _boy.selected = NO;
        _gender = @"unknow";  // 默认不选性别的时候为保密
    } else {
        _gender = @"male";
        [_boy setImage:[UIImage imageNamed:@"gender_selected_btn.png"] forState:UIControlStateSelected];
        _boy.selected = YES;
        [_girl setImage:[UIImage imageNamed:@"gender_default_btn.png"] forState:UIControlStateNormal];
        _girl.selected = NO;
        [_secrecy setImage:[UIImage imageNamed:@"gender_default_btn.png"] forState:UIControlStateNormal];
        _secrecy.selected = NO;
    }
}
- (IBAction)girl:(id)sender {
    if (_girl.selected) {
        [_girl setImage:[UIImage imageNamed:@"gender_default_btn.png"] forState:UIControlStateNormal];
        _girl.selected = NO;
        _gender = @"unknow";  // 默认不选性别的时候为保密
    } else {
        _gender = @"female";
        [_girl setImage:[UIImage imageNamed:@"gender_selected_btn.png"] forState:UIControlStateSelected];
        _girl.selected = YES;
        [_boy setImage:[UIImage imageNamed:@"gender_default_btn.png"] forState:UIControlStateNormal];
        _boy.selected = NO;
        [_secrecy setImage:[UIImage imageNamed:@"gender_default_btn.png"] forState:UIControlStateNormal];
        _secrecy.selected = NO;
    }
}
- (IBAction)secrecy:(id)sender {
    if (_secrecy.selected) {
        [_secrecy setImage:[UIImage imageNamed:@"gender_default_btn.png"] forState:UIControlStateNormal];
        _secrecy.selected = NO;
        _gender = @"unknow";  // 默认不选性别的时候为保密
    } else {
        _gender = @"unknow";
        [_secrecy setImage:[UIImage imageNamed:@"gender_selected_btn.png"] forState:UIControlStateNormal];
        _secrecy.selected = YES;
        [_boy setImage:[UIImage imageNamed:@"gender_default_btn.png"] forState:UIControlStateNormal];
        _boy.selected = NO;
        [_girl setImage:[UIImage imageNamed:@"gender_default_btn.png"] forState:UIControlStateNormal];
        _girl.selected = NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
